package com.uhylab.uhuvinetest.utils;

import android.widget.ImageView;

/**
 * Created by leige07 on 2016/4/14 0014.
 */
public class SceneAnimation {

    private ImageView mImageView;
    private int[] mFrameRess;
    private int[] mDurations;
    private int mDuration;

    private int mLastFrameNo;
    private long mBreakDelay;

    private boolean is_stop = false;

    public SceneAnimation(ImageView pImageView, int[] pFrameRess,
                          int[] pDurations) {
        mImageView = pImageView;
        mFrameRess = pFrameRess;
        mDurations = pDurations;
        mLastFrameNo = pFrameRess.length - 1;

        mImageView.setBackgroundResource(mFrameRess[0]);
        play(1);
    }

    public SceneAnimation(ImageView pImageView, int[] pFrameRess, int pDuration) {
        mImageView = pImageView;
        mFrameRess = pFrameRess;
        mDuration = pDuration;
        mLastFrameNo = pFrameRess.length - 1;

        mImageView.setBackgroundResource(mFrameRess[0]);
        playConstant(1);
    }

    public SceneAnimation(ImageView pImageView, int[] pFrameRess,
                          int pDuration, long pBreakDelay) {
        mImageView = pImageView;
        mFrameRess = pFrameRess;
        mDuration = pDuration;
        mLastFrameNo = pFrameRess.length - 1;
        mBreakDelay = pBreakDelay;

        mImageView.setBackgroundResource(mFrameRess[0]);
        playConstant(1);
    }

    private void play(final int pFrameNo) {
        if (!is_stop) {
            if (mImageView != null) {


                mImageView.postDelayed(new Runnable() {
                    public void run() {
                        if (mImageView != null) {
                            mImageView.setBackgroundResource(mFrameRess[pFrameNo]);
                            if (pFrameNo == mLastFrameNo)
                                play(0);
                            else
                                play(pFrameNo + 1);
                        }
                    }
                }, mDurations[pFrameNo]);
            }
        }
    }

    public void stop() {
        if (mImageView != null) {
            is_stop = true;
            mImageView = null;
        }

    }

    private void playConstant(final int pFrameNo) {
        mImageView.postDelayed(new Runnable() {
            public void run() {
                mImageView.setBackgroundResource(mFrameRess[pFrameNo]);

                if (pFrameNo == mLastFrameNo)
                    playConstant(0);
                else
                    playConstant(pFrameNo + 1);
            }
        }, pFrameNo == mLastFrameNo && mBreakDelay > 0 ? mBreakDelay
                : mDuration);
    }

}
